@extends('home.parent')
@section('title','新增管理员角色')
@section('head_js')
@endsection
@section('head_css')
@endsection
@section('content')
<div class="content-wrapper">
<section class="content-header">
  <h1>
    新增管理员角色
    <small></small>
  </h1>
  <ol class="breadcrumb">
    <li><a href="{{URL('admin')}}"><i class="fa fa-dashboard"></i> 首页</a></li>
    <li><a href="{{URL('admin/role/list')}}">管理员角色列表</a></li>
    <li class="active">新增管理员角色</li>
  </ol>
</section>
<section class="content">
  <div class="row">
    <div class="col-md-10">
      <!-- general form elements -->
      <div class="box box-primary">
        <!--
        <div class="box-header with-border">
          <h3 class="box-title"></h3>
        </div>
        -->
        <!-- /.box-header -->
        <!-- form start -->
        @if(Session::has('error_msg'))
        <div class="callout callout-danger" id="login-box-msg" style="display:block;">
            <h4 id="show_error_msg">{{ session('error_msg') }}</h4>
        </div>
        @else
        <div class="callout callout-danger" id="login-box-msg" style="display:none;">
            <h4 id="show_error_msg"></h4>
        </div>
        @endif
        <form role="form" action="{{URL('admin/role/create/save')}}" method="post" id="admin_role_create_form">
          <div class="box-body">
            <div class="form-group">
              <label for="role_name">角色名</label>
              <input type="text" class="form-control" id="role_name" name="role_name" placeholder="请输入角色名" required>
            </div>
            <div class="form-group">
              <label for="limits_ids">权限</label>
              <input type="hidden" class="form-control" id="limits_ids" name="limits_ids">
            </div>
            @foreach($data as $key => $limit)
            <div class="col-md-3">
              <div @if($key > 0) class="box box-solid collapsed-box" @else class="box box-solid" @endif>
                <div class="box-header with-border">
                  <h3 class="box-title">{{ $limit['limit_name'] }}</h3>
                  <div class="box-tools">
                    <button type="button" class="btn btn-box-tool" data-widget="collapse"><i @if($key > 0) class="fa fa-plus" @else class="fa fa-minus" @endif></i>
                    </button>
                  </div>
                </div>
                @if(!empty($limit['sons']))
                <div class="box-body no-padding" @if($key > 0) style="display: none;" @else style="display:block;" @endif>
                  <ul class="nav nav-pills nav-stacked">
                    @foreach($limit['sons'] as $son)
                    <li><a href="javascript:;"><input type="checkbox" class="limit_url_checkbox" value="{{ $son['admin_limit_id'] }}"> {{ $son['limit_name'] }}</a></li>
                    @endforeach
                  </ul>
                </div>
                @endif
              </div>
            </div>
            @endforeach
          </div>
          <div class="box-footer">
            <button type="submit" class="btn btn-primary">提 交</button>
            <button type="button" class="btn btn-danger pull-right" onclick="history.go(-1)">取 消</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</section>
</div>
@endsection
@section('foot_js')
<script src="{{ asset('/js/tools.js') }}"></script>
<script type="text/javascript">
$(document).ready(function(){
  $('#admin_role_create_form').submit(function(){
    var role_name  =  $.trim($("#role_name").val());
    if(role_name == ''){
      $('#show_error_msg').html('请输入角色名');
      $("#login-box-msg").show();
      return false;
    }

    var limits_ids;
    var limits_ids_arr = [];
    $(".limit_url_checkbox").each(function(){
      if($(this).is(':checked')){
        limits_ids_arr.push($(this).val());
      }
    });
    if(limits_ids_arr != ''){
      limits_ids = limits_ids_arr.join(',');
      $('#limits_ids').val(limits_ids);
    }else{
      $("#show_error_msg").html('请勾选权限');
      $("#login-box-msg").show();
      return false;
    }

    var url = "{{URL('admin/role/valid/rolename')}}";
    var data = {role_name:role_name};
    var is_success = puppet.myajax('post',url,data,false);
    if(is_success.code == true){
      $("#show_error_msg").html(is_success.message);
      $("#login-box-msg").show();
      return false;
    }
    layer.msg('操作成功',{icon:1});
  })
})
</script>
@endsection